package com.boc.langxiang.ui.activity.shop;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.common.AddressManageContract;
import com.boc.langxiang.entity.response.AddressEntity;
import com.boc.langxiang.entity.response.AddressListContent;
import com.boc.langxiang.entity.response.BaseResponse;
import com.boc.langxiang.presenter.common.AddressManagePresenter;
import com.boc.langxiang.ui.activity.BaseToolBarActivity;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class AddressSelectorActivity extends BaseToolBarActivity implements AddressManageContract.View {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    private BaseQuickAdapter<AddressEntity> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_selector);
        ButterKnife.bind(this);
        initViews();
    }

    private void initViews() {
        mAdapter = new BaseQuickAdapter<AddressEntity>( R.layout.item_address_selector, new ArrayList<AddressEntity>()) {
            @Override
            protected void convert(BaseViewHolder helper, final AddressEntity item) {
                helper.setText(R.id.name_tv, item.getUsername())
                        .setText(R.id.phone_tv, item.getPhone())
                        .setText(R.id.address_tv, item.getAddress());
                if (helper.getPosition() != 0) {
                    helper.getView(R.id.top_divider).setVisibility(View.GONE);
                }
                helper.getConvertView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.putExtra("data", item);
                        setResult(RESULT_OK, intent);
                        finish();
                    }
                });
            }
        };
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);

        initData();
    }

    private void initData() {
        new AddressManagePresenter(this).getAddressList();
    }

    @Override
    protected void onCreateCustomToolBar(Toolbar toolbar) {
        super.onCreateCustomToolBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.icon_return_black);
        toolbar.setBackgroundColor(Color.WHITE);
        setToolBarTitle("選擇地址");
    }

    @Override
    protected boolean isSetTypeFace() {
        return true;
    }

    @Override
    public void onGetAddressSuccess(BaseResponse<AddressListContent> response) {
        if (response.getContent().getAddrList().size() > 0) {
            mAdapter.setNewData(response.getContent().getAddrList());
        }
    }

    @Override
    public void showDeleteLoading() {

    }

    @Override
    public void onDeleteAddressSuccess() {

    }

    @Override
    public void setDefaultAddr(int id) {

    }

    @Override
    public void showSetDefaultLoading() {

    }

    @Override
    public void setDefaultAddressSuccess() {

    }

    @Override
    public void showLoading() {
        showProgressDialog("加载中，请稍候....");
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        T.showShort(mContext, msg);
    }
}
